<?php
class jxlist extends admin{
    
    public function __construct()
    {
        parent::__construct();
        array_walk_recursive($_GET, 'safe_filter');
        array_walk_recursive($_POST, 'safe_filter');
    }
    //VUE.JS 删除驾校 数据接口:
    public function jx_delete(){
        $id = (int)$_POST['jx_id']; //驾校id
        $delete = db()->delete('x_jiaxiaoinfo', 'id=' . $id);
        if ($delete) {
            $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
            $infoid = $id;
            $method = 'delete';
            $mid = 11;              //所属模块
            $remark = '删除平台驾校信息(驾校ID:'.$id.')';
            insertlogs($userData,$logs='',$infoid,$method,$mid,$remark);
            $return = array('status' => 1, 'info' => '删除成功');
        }else{
            $return = array('status'=>0,'info' =>'系统繁忙,请稍后重试');
        }
        echo json_encode($return);
    }

    //平台图片上传函数;
    public function jxpicture(){
        if($_FILES['error']>0){
            echo '上传的文件有误';
        }
        //把图片移动指定目录
        $path = 'upload/pt_jiaxiao/'.date("Ymd").'/';
        mkDirs($path);
        $file = $_FILES['file']['name'];
        $extension=substr($file, strrpos($file, '.')+1); //后缀名;
        $path.=mt_rand(1000,9999).md5($_FILES['file']['tmp_name']).'.'.$extension;
        $path = iconv('UTF-8','GB2312',$path);
        //参数1:默认地址,参数2:移动的新地址;
        if(move_uploaded_file($_FILES['file']['tmp_name'],$path)){
            //进入此处,移动成功;
            $return = array('success'=>1,'path'=>$path);
        }else{
            $return = array('success'=>0,'path'=>'图片上传失败');
        }
        echo json_encode($return);
    }
    //vue.js 添加驾校 数据接口:
    public function pt_jxadd(){
//        echo json_encode(array('status'=>1,'data'=>$_POST));
        $data['name'] = trim($_POST['jx_name']);
        $data['addtime']=date('Y-m-d H:i:s');
        $data['address'] = trim($_POST['jx_address']);
        $data['proxytypes'] = trim($_POST['jx_level']);
        $data['tel']        = intval($_POST['jx_mobile']);
        $data['picture']    = trim($_POST['jx_picture']);
        $data['price']      = $_POST['jx_price'];
        $data['aid']        = $_POST['selected'];
        $data['shuoming']   = $_POST['textarea'];
        $data['ecityid']     = 1703;
        $ex = explode(',',$_POST['info']);
        $data['baidu_lng'] = $ex['0'];
        $data['baidu_lat'] = $ex['1'];
        $insert_id=db()->insert('x_jiaxiaoinfo',$data);
        $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
        $logs = json_encode($data);
        $infoid = $insert_id;
        $method = 'create';
        $mid = 11;
        $remark = '添加驾校信息(驾校ID:'.$insert_id.')';
        insertlogs($userData,$logs,$infoid,$method,$mid,$remark);
        if($insert_id) $return = array('status'=>1,'info'=>'添加成功');
        else $return = array('status'=>0,'info'=>'添加失败');
        echo json_encode($return);
    }
    //vue.js 添加驾校获取下拉区域数据 数据接口:
    public function tr_area(){
        $areas = db()->getAll("select aid,aName from x_e_area WHERE cid = '1703'");
        echo json_encode(array('status'=>1,'data'=>$areas));
    }

    public function jaxao()
    {
        $JxAddress = $_POST['jx_address'];//获取驾校名称;
        $url = "http://api.map.baidu.com/geocoder/v2/?address=" . $JxAddress . "&output=json&ak=CEe2a28a40d5546063dd868beef108c9";
        $json = curl_get_file_contents($url);
        if(!$json){
            echo '坐标未能获取,检查驾校名和驾校地址';
        };
        $preg='/:(\d+\.\d+).+:(\d+\.\d+)}/';
        if(!preg_match($preg,$json,$match)){
            echo '未能匹配';
        };
        array_shift($match);
        echo json_encode(array('status'=>1,'data'=>$match));
    }
    public function index()
    {
        $tpl = $this->Template();
		$pagesize=10;
		$url = '/admin/jxlist/';
        //		$where = ' where 1=1 ';
		if($_GET['keyword']!=''){
			$keyword = trim($_GET['keyword']);
			$where = " and name like '%$keyword%' ";
			$url .= '?keyword='.$keyword;
		}
		$one = "select count(*) from x_jiaxiaoinfo WHERE ecityid='1703'".$where;
		$count = db()->getOne($one);
		$page = $_GET['page']>0?$_GET['page']:1;
		$pages = ceil($count/$pagesize);
		if($page>=$pages) $page = $pages;
		$start = ($page-1)*$pagesize;
		$sql2 = "select id,name,address,tel,aid,price,cityname,anames,addtime from x_jiaxiaoinfo WHERE ecityid='1703' ".$where." order by id desc limit $start,$pagesize";
//		echo $sql2;
        $list = db()->getAll($sql2);
		if(count($list)>0){
			foreach($list AS $k=>$v){
				$sql = "select aName from x_e_area where aid='$v[aid]' and cid='1703'";
				$ainfo = db()->getRow($sql);
				$list[$k]['aName'] = $ainfo['aName'];
			}
		}

        //$area = db()->getRow("select upid from area WHERE ecityid = 1703");
        //$areas = db()->getAll("select * from area WHERE upid=".$area['upid']);
		//<div class="layui-box layui-laypage layui-laypage-default" id="layui-laypage-0"><span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>1</em></span><a href="javascript:;" data-page="2">2</a><a href="javascript:;" data-page="3">3</a><a href="javascript:;" data-page="4">4</a><a href="javascript:;" data-page="5">5</a><span>…</span><a href="javascript:;" class="layui-laypage-last" title="尾页" data-page="100">末页</a><a href="javascript:;" class="layui-laypage-next" data-page="2">下一页</a></div>
		$pages_s=pages($pagesize,$count,$url);
        $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));

//        $tpl->assign('pages', $pages_s);
//        $tpl->assign('list', $list);
        //        //$tpl->assign('areas', $areas);
//        $tpl->display();
    }
    
    public function add()
    {
        if(IS_AJAX&&IS_POST){
//            if(!isset($_POST['name'])||empty($_POST['name'])) $this->ajaxReturn(array('status'=>'n','info'=>'请填写驾校名称'));
//            if(!isset($_POST['address'])||empty($_POST['remark'])) $this->ajaxReturn(array('status'=>'n','info'=>'请填写驾校学车说明内容'));
            $data['name']=trim($_POST['name']);
            $data['address'] = trim($_POST['address']);
            $data['tel'] = $_POST['tel'];
            $data['price'] = $_POST['price'];
            $data['aid'] = $_POST['aid'];
            $data['addtime']=date('Y-m-d H:i:s');
            $data['picture']     = $_POST['picture'];           //车辆图片地址;
            $data['shuoming']    = $_POST['shuoming'];
            $data['content']     = $_POST['content'];
            $data['ecityid']     = 1703;
            $data['proxytypes']  = $_POST['propres'];
            $ex = explode(',',$_POST['location']);
            $data['baidu_lng'] = $ex['0'];
            $data['baidu_lat'] = $ex['1'];

            $insert_id=db()->insert('x_jiaxiaoinfo',$data);
			$userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
			$logs = json_encode($data);
			$infoid = $insert_id;
			$method = 'create';
			$mid = 11;
			$remark = '添加驾校信息(驾校ID:'.$insert_id.')';
			insertlogs($userData,$logs,$infoid,$method,$mid,$remark);
            if($insert_id) $this->ajaxReturn(array('status'=>'y','info'=>'添加成功'));
            else $this->ajaxReturn(array('status'=>'n','info'=>'添加失败'));
        }else{
            $tpl=$this->Template();
            $areas = db()->getAll("select aid,aName from x_e_area WHERE cid = '1703'");
            $tpl->assign('areas', $areas);
            $tpl->display('jxlist/add.html');
        }
    }
    
    public function edit()
    {
        $tpl = $this->Template();

        if(IS_AJAX&&IS_POST){
            $id=(int)$_POST['id'];
            if(!isset($_POST['name'])||empty($_POST['name'])) $this->ajaxReturn(array('status'=>'n','info'=>'更新此信息有误'));
            if(!isset($_POST['address'])||empty($_POST['address'])) $this->ajaxReturn(array('status'=>'n','info'=>'更该此信息有误,请重新填写'));
			$data['id']=$_POST['id'];
            $data['name']=trim($_POST['name']);
            $data['address'] = trim($_POST['address']);
            $data['tel'] = $_POST['tel'];
            $data['price'] = $_POST['price'];
            $data['ecityid'] = $_POST['ecityid']='1703';
            $data['aid']=$_POST['aid'];
            $data['picture'] = $_POST['picture'];
            $data['shuoming']    = $_POST['shuoming'];
            $data['content']     = $_POST['content'];
            $data['proxytypes']  = $_POST['propres'];
            //$data['addtime']=date('Y-m-d H:i:s');
            $ex = explode(',',$_POST['location']);
            $data['baidu_lng'] = $ex['0'];
            $data['baidu_lat'] = $ex['1'];
            $insert_id=db()->update('x_jiaxiaoinfo',$data,'id='.$id);
            if($insert_id){
                //file_put_contents('123.log',$insert_id);
                $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
                $logs = json_encode($data);
                $infoid = $id;
                $method = 'update';
                $mid = 11;
                $remark = '修改驾校信息(驾校ID:'.$id.')';
                insertlogs($userData,$logs,$infoid,$method,$mid,$remark);

                $this->ajaxReturn(array('status'=>'y','info'=>'更新成功'));
            }else $this->ajaxReturn(array('status'=>'n','info'=>'更新失败'));

            }else{
            $id=(int)$_GET['id'];
            $res = db()->getRow("select id,ecityid,name,aid,address,proxytypes,baidu_lng,baidu_lat,tel,price,picture,shuoming,content from x_jiaxiaoinfo WHERE id=" . $id);
            $areas = db()->getAll("select aid,aName from x_e_area WHERE cid = '1703'");

            $tpl->assign('areas', $areas);
            $tpl->assign('res', $res);
            $tpl->display('jxlist/edit.html');
        }
    }

    public function info()
    {
            $tpl = $this->Template();
            $id=(int)$_GET['id'];
            $res = db()->getRow("select id,ecityid,name,aid,address,proxytypes,baidu_lng,baidu_lat,tel,price,picture,shuoming,content FROM x_jiaxiaoinfo WHERE id=".$id);
            $areas = db()->getAll("select aid,aName from x_e_area WHERE cid = '1703'");
			$tpl->assign('areas', $areas);
            $tpl->assign('res', $res);

			$sql = "select id,real_name,mobile,id_number,age from x_user where school_id='$id' and user_type=2";
			$jiaolians = db()->getAll($sql);
			$tpl->assign('jiaolians', $jiaolians);

			$sql = "select id,real_name,age,id_number from x_user where school_id='$id'";
			$jiaolians = db()->getAll($sql);
			$tpl->assign('jiaolians', $jiaolians);

			$sql = "select id,car_model,car_num,is_kt from jx_bus where jx_id='$id'";
			$bus = db()->getAll($sql);
			if(count($bus)>0){
				foreach($bus AS $k=>$v){
					if($v['is_kt']==1){
						$bus[$k]['is_kt'] = '是';
					}
					if($v['is_kt']==0){
						$bus[$k]['is_kt'] = '否';
					}
				}
			}

			$jx_img = db()->getAll("select id,jx_id,title,picture from x_jx_picture WHERE jx_id =".$id);
            $tpl->assign('jx_img', $jx_img);
            $tpl->assign('jxImg',json_encode($jx_img));
			$tpl->display();
    }

    public function del()
    {
        if (IS_AJAX) {
            $id = (int)$_GET['id'];
            $delete = db()->delete('x_jiaxiaoinfo', 'id=' . $id);
            if ($delete) {
                $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
                $infoid = $id;
                $method = 'delete';
                $mid = 11;      //所属模块
                $remark = '删除驾校信息(驾校ID:'.$id.')';
                insertlogs($userData,$logs='',$infoid,$method,$mid,$remark);
            $this->ajaxReturn(array('status' => 'y', 'info' => '删除成功'));
            }else{
                $this->ajaxReturn(array('status' => 'n', 'info' => '删除失败'));
            }
        }
    }

    public function jxconfig(){
        
    }    
    
    public function curl()
    {
        $val = isset($_POST['val'])?$_POST['val'] : '';
        //   echo json_encode($val);

        // $url = "http://api.map.baidu.com/geocoder/v2/?address=" . $_POST['val'] . "&city=" . "海城市" . "&output=json&ak=CEe2a28a40d5546063dd868beef108c9";
        $url = "http://api.map.baidu.com/geocoder/v2/?address=" . $val . "&output=json&ak=CEe2a28a40d5546063dd868beef108c9";
        $json = curl_get_file_contents($url);
        if(!$json){
            echo '坐标未能获取,检查驾校名和驾校地址';
        };
        $preg='/:(\d+\.\d+).+:(\d+\.\d+)}/';
        if(!preg_match($preg,$json,$match)){
            echo '未能匹配';
        };
            array_shift($match);
        echo json_encode($match);
    }

//省份列表
    public function ld(){
        $province = db()->getAll("select areaid,areaname from area WHERE rank = 2 and is_china = 1");
        foreach ($province as $prolist){
            $pro_name = $prolist['areaname'];
            $pro_id   = $prolist['areaid'];
            echo "<option value='".$pro_id."'>$pro_name</option>";
        }
    }

    //    城市列表
    public function city(){
    $pid = $_POST['pid'];
    $city = db()->getAll("select areaid,areaname from area WHERE rank=3 and  upid=".$pid);

        foreach ($city as $ct){
            $city_name = $ct['areaname'];
            $city_id   = $ct['areaid'];
            echo "<option value='".$city_id."'>$city_name</option>";
        }
    }

    public function area(){
        $cityid = $_POST['cityid'];
        $area = db()->getAll("select areaid,areaname from area WHERE upid=".$cityid);
            foreach($area as $ar){
                $ar_id = $ar['areaid'];
                $ar_name = $ar['areaname'];
                echo "<option value='".$ar_id."'>$ar_name</option>";
            }
    }


    public function uploads()
    {
        if($_FILES['error']>0){
            echo '上传的文件有误';
        }
        //把图片移动指定目录
        $path = 'upload/jiaxiao/'.date("Ymd").'/';
		mkDirs($path);
		$file = $_FILES['file']['name'];
		$extension=substr($file, strrpos($file, '.')+1);
        $path.=mt_rand(1000,9999).md5($_FILES['file']['tmp_name']).'.'.$extension;
        $path = iconv('UTF-8','GB2312',$path);
        //参数1:默认地址,参数2:移动的新地址;
        if(move_uploaded_file($_FILES['file']['tmp_name'],$path)){
            //进入此处,移动成功;
            $upload_image = '/usr/local/apache2/htdocs/xuechekuaishou/'.$path;
            $pic_type = 'jiaxiao';
            $imagePath = date('Ymd');
            $uppic = uploadpic($upload_image, $imagePath, $pic_type);
            if ($uppic['status'] == 0 && $uppic['data']['imageName'] != '') {
                $imgg2 = '/upload/' . $pic_type . '/' . $imagePath . '/' . $uppic['data']['imageName'];
                echo  json_encode(array('status'=>'ok','info'=>$imgg2));
            }else{
                echo '上传失败';
            }
        } else{
            echo  json_encode(array('status'=>'n','info'=> '上传失败'));
        }
    }

    //多图上传:
    public function uploadsPicture(){
        $file = $_FILES['myfile'];  //得到传输的数据,以数组的形式
        $name = $file['name'];      //得到文件名称，以数组的形式
        $upload_path = 'upload/jiaxiaos/'.date("Ymd").'/';   //图片上传的路径;
        mkDirs($upload_path);
        foreach($name as $k => $names){
            $type = strtolower(substr($names,strrpos($names,'.')+1)); //得到.后缀文件名,并且转化为小写;
            $allow_type = array('jpg','jpeg','gif','png');
            //把非法字符去除;
            if(!in_array($type,$allow_type)){
                unset($names[$k]);
            }
        }
        foreach ($name as $k=>$item){
            $type = strtolower(substr($item,strrpos($item,'.')+1));//得到文件类型,并且转化为小写;
            $image_md5 = substr(md5($item), 16, 16);
            $path = $upload_path.$image_md5.'.'.$type;
            if(move_uploaded_file($file['tmp_name'][$k],$path)){
                echo json_encode($path);
            }else{
                echo 'no';
            }
        }
    }
    

}

































